package weavingdraft;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

/**
 * Creates the tie up grid allowing users to interact with it. 
 * @author Justin Wong
 *
 */
@SuppressWarnings("serial")
public class TieUpSquare extends JPanel{
	/**
	 * The row this object refers to. 
	 */
	private int my_row;
	/**
	 * The column this object refers to. 
	 */
	private int my_column;
	/**
	 * The WeavingDraft this object refers to. 
	 */
	private WeavingDraft my_draft;
	/**
	 * The number of clicks. 
	 */
	private int number;
	/**
	 * Creates the squares for the TieUpGrid. 
	 * @param the_row the row this object refers to. 
	 * @param the_column the column this object refers to. 
	 * @param the_tieup the TieUpGrid this object refers to. 
	 * @param the_draft the draft this object refers to. 
	 */
	public TieUpSquare(int the_row, int the_column, TieUpGrid the_tieup, WeavingDraft the_draft){
		my_row = the_row;
		my_column = the_column;
		my_draft = the_draft;
		number = 0;
		this.addMouseListener(new MouseAction(this));
	}
	/**
	 * Paints the JPanel object and updates when necessary. 
	 */
	@Override
	public void paintComponent(Graphics the_graphics){
		Graphics2D variable_graphics = (Graphics2D) the_graphics;
		int width = this.getWidth();
		int height = this.getHeight();
		
		if(number % 2 == 0){
			variable_graphics.setColor(Color.WHITE);
			variable_graphics.fillRect(0, 0, width-1, height-1);
			variable_graphics.setColor(Color.BLACK);
			variable_graphics.drawRect(0, 0, width-1, height-1);
			
			my_draft.deleteTieUp(my_row, my_column);
		}else{
			variable_graphics.setColor(Color.BLACK);
			variable_graphics.fillRect(0, 0, width-1, height-1);
			variable_graphics.setColor(Color.WHITE);
			variable_graphics.drawRect(0, 0, width-1, height-1);
			
			my_draft.setTieUp(my_row, my_column);
		}
		
	}
	/**
	 * Attaches the action listener to this object. 
	 * @author Justin Wong
	 *
	 */
	class MouseAction extends MouseInputAdapter{
		/**
		 * The panel this object refers to. 
		 */
		private JPanel my_panel;
		/**
		 * Creates and attaches the actionlistener to the JPanel. 
		 * @param the_panel the object refers to. 
		 */
		private MouseAction(JPanel the_panel){
			my_panel = the_panel;
		}
		/**
		 * The actions to take when the mouse is clicked. 
		 */
		@Override
		public void mouseClicked(MouseEvent e){
			number++;
			my_panel.repaint();
		}
	}
}
